{% extends 'base.html' %}
{% load static %}

{% block title %}帖子列表 - 本地有约{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="row">
        <!-- 主要内容 -->
        <div class="col-lg-9">
            <!-- 筛选栏 -->
            <div class="card mb-4">
                <div class="card-body">
                    <div class="row g-3 align-items-center">
                        <div class="col-md-4">
                            <select class="form-select" id="typeFilter" onchange="filterPosts()">
                                <option value="">全部类型</option>
                                <option value="discussion" {% if selected_type == 'discussion' %}selected{% endif %}>讨论</option>
                                <option value="experience" {% if selected_type == 'experience' %}selected{% endif %}>经验分享</option>
                                <option value="question" {% if selected_type == 'question' %}selected{% endif %}>提问</option>
                                <option value="announcement" {% if selected_type == 'announcement' %}selected{% endif %}>公告</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <select class="form-select" id="categoryFilter" onchange="filterPosts()">
                                <option value="">全部分类</option>
                                {% for category in categories %}
                                <option value="{{ category.id }}" {% if selected_category == category.id|stringformat:"i" %}selected{% endif %}>
                                    {{ category.name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-4">
                            <select class="form-select" id="sortFilter" onchange="filterPosts()">
                                <option value="newest" {% if selected_sort == 'newest' %}selected{% endif %}>最新发布</option>
                                <option value="hot" {% if selected_sort == 'hot' %}selected{% endif %}>最热帖子</option>
                                <option value="commented" {% if selected_sort == 'commented' %}selected{% endif %}>最多评论</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 帖子列表 -->
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">
                        <i class="fas fa-list me-2"></i>帖子列表
                        <span class="badge bg-primary ms-2">{{ total_posts }}</span>
                    </h5>
                    {% if user.is_authenticated %}
                    <a href="{% url 'location:create_post' %}" class="btn btn-primary btn-sm">
                        <i class="fas fa-plus me-1"></i>发布帖子
                    </a>
                    {% endif %}
                </div>
                <div class="card-body">
                    {% for post in posts %}
                    <div class="post-item mb-4 pb-4 border-bottom">
                        <div class="d-flex align-items-start">
                            <img src="{{ post.author.avatar.url|default:'/static/images/default-avatar.jpg' }}"
                                 class="rounded-circle me-3" width="48" height="48" alt="头像">
                            <div class="flex-grow-1">
                                <h5 class="mb-2">
                                    <a href="{% url 'location:post_detail' post.id %}" class="text-decoration-none text-dark">
                                        {{ post.title }}
                                    </a>
                                    {% if post.is_pinned %}
                                    <span class="badge bg-warning ms-2">置顶</span>
                                    {% endif %}
                                    {% if post.is_featured %}
                                    <span class="badge bg-success ms-1">精选</span>
                                    {% endif %}
                                </h5>

                                <p class="text-muted mb-3">
                                    {{ post.content|striptags|truncatechars:150 }}
                                </p>

                                <div class="d-flex justify-content-between align-items-center">
                                    <div class="d-flex align-items-center">
                                        <span class="text-muted me-3">
                                            <i class="fas fa-user me-1"></i>{{ post.author.username }}
                                        </span>
                                        <span class="text-muted me-3">
                                            <i class="fas fa-clock me-1"></i>{{ post.created_at|timesince }}前
                                        </span>
                                        {% if post.event %}
                                        <span class="badge bg-info me-2">
                                            <i class="fas fa-calendar me-1"></i>{{ post.event.category.name }}
                                        </span>
                                        {% endif %}
                                        <span class="badge bg-secondary">{{ post.get_post_type_display }}</span>
                                    </div>

                                    <div class="text-muted">
                                        <span class="me-3">
                                            <i class="fas fa-eye me-1"></i>{{ post.views_count }}
                                        </span>
                                        <span class="me-3">
                                            <i class="fas fa-comment me-1"></i>{{ post.comments_count }}
                                        </span>
                                        <span>
                                            <i class="fas fa-heart me-1"></i>{{ post.likes_count }}
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% empty %}
                    <div class="text-center py-5">
                        <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">暂无帖子</h5>
                        <p class="text-muted mb-4">还没有人发布帖子，快来发布第一个帖子吧！</p>
                        {% if user.is_authenticated %}
                        <a href="{% url 'location:create_post' %}" class="btn btn-primary">发布帖子</a>
                        {% else %}
                        <a href="{% url 'customer:login' %}" class="btn btn-primary">登录后发布</a>
                        {% endif %}
                    </div>
                    {% endfor %}

                    <!-- 分页 -->
                    {% if page_obj.has_other_pages %}
                    <nav aria-label="帖子分页" class="mt-4">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if selected_type %}&type={{ selected_type }}{% endif %}{% if selected_category %}&category={{ selected_category }}{% endif %}{% if selected_sort %}&sort={{ selected_sort }}{% endif %}">
                                    <i class="fas fa-chevron-left"></i>
                                </a>
                            </li>
                            {% endif %}

                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                <li class="page-item active">
                                    <span class="page-link">{{ num }}</span>
                                </li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ num }}{% if selected_type %}&type={{ selected_type }}{% endif %}{% if selected_category %}&category={{ selected_category }}{% endif %}{% if selected_sort %}&sort={{ selected_sort }}{% endif %}">{{ num }}</a>
                                </li>
                                {% endif %}
                            {% endfor %}

                            {% if page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if selected_type %}&type={{ selected_type }}{% endif %}{% if selected_category %}&category={{ selected_category }}{% endif %}{% if selected_sort %}&sort={{ selected_sort }}{% endif %}">
                                    <i class="fas fa-chevron-right"></i>
                                </a>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- 右侧边栏 -->
        <div class="col-lg-3">
            <!-- 发布帖子 -->
            {% if user.is_authenticated %}
            <div class="card mb-4">
                <div class="card-body text-center">
                    <a href="{% url 'location:create_post' %}" class="btn btn-primary w-100 mb-2">
                        <i class="fas fa-edit me-2"></i>发布帖子
                    </a>
                    <small class="text-muted">分享你的活动心得和经验</small>
                </div>
            </div>
            {% endif %}

            <!-- 帖子类型统计 -->
            <div class="card mb-4">
                <div class="card-header">
                    <h6 class="mb-0"><i class="fas fa-chart-pie me-2"></i>帖子类型</h6>
                </div>
                <div class="card-body">
                    <div class="list-group list-group-flush">
                        <a href="?type=discussion" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">
                            讨论
                            <span class="badge bg-primary rounded-pill">{{ discussion_count }}</span>
                        </a>
                        <a href="?type=experience" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">
                            经验分享
                            <span class="badge bg-success rounded-pill">{{ experience_count }}</span>
                        </a>
                        <a href="?type=question" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">
                            提问
                            <span class="badge bg-warning rounded-pill">{{ question_count }}</span>
                        </a>
                        <a href="?type=announcement" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">
                            公告
                            <span class="badge bg-info rounded-pill">{{ announcement_count }}</span>
                        </a>
                    </div>
                </div>
            </div>

            <!-- 热门标签 -->
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0"><i class="fas fa-tags me-2"></i>热门分类</h6>
                </div>
                <div class="card-body">
                    <div class="d-flex flex-wrap gap-2">
                        {% for category in categories %}
                        <a href="?category={{ category.id }}" class="badge bg-light text-dark text-decoration-none">
                            {{ category.name }}
                        </a>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function filterPosts() {
    const type = document.getElementById('typeFilter').value;
    const category = document.getElementById('categoryFilter').value;
    const sort = document.getElementById('sortFilter').value;

    let url = '{% url "location:community_posts" %}?';
    const params = [];

    if (type) params.push(`type=${type}`);
    if (category) params.push(`category=${category}`);
    if (sort) params.push(`sort=${sort}`);

    window.location.href = url + params.join('&');
}
</script>

<style>
.post-item:hover {
    background-color: #f8f9fa;
    border-radius: 8px;
    margin: -8px;
    padding: 8px;
}

.list-group-item {
    border: none;
    padding: 0.75rem 0;
}

.list-group-item:first-child {
    padding-top: 0;
}

.list-group-item:last-child {
    padding-bottom: 0;
}
</style>
{% endblock %}